package easier.fast.page.pull;

import static easier.app.x.CommonCode.getStatusBarHeight;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import easier.app.x.CommonCode;
import easier.fast.R;

public class TitleCode {

    public static void initTitleAction( Activity activity, CharSequence titleString ) {

        initTitleAction( activity, titleString, null );
    }

    public static void initTitleAction( Activity activity, CharSequence titleString, @Nullable CharSequence rightTitleString ) {

        View space = activity.findViewById( R.id.space );
        View back = activity.findViewById( R.id.back );
        View title = activity.findViewById( R.id.title );
        View rightTitle = activity.findViewById( R.id.rightTitle );
        View rightImage = activity.findViewById( R.id.rightImage );

        initTitleAction( activity, 0, space, back, title, rightTitle, rightImage, titleString, rightTitleString, 0 );
    }

    public static void initTitleAction( Fragment fragment, View view, CharSequence titleString, @DrawableRes int rightImageRes ) {

        View space = view.findViewById( R.id.space );
        View back = view.findViewById( R.id.back );
        View title = view.findViewById( R.id.title );
        View rightTitle = view.findViewById( R.id.rightTitle );
        View rightImageView = view.findViewById( R.id.rightImage );

        initTitleAction( fragment.getContext(), 1, space, back, title, rightTitle, rightImageView, titleString, null,
            rightImageRes );
    }


    public static void initTitleAction( Fragment fragment, View view, CharSequence titleString, CharSequence rightTitleString ) {

        View space = view.findViewById( R.id.space );
        View back = view.findViewById( R.id.back );
        View title = view.findViewById( R.id.title );
        View rightTitle = view.findViewById( R.id.rightTitle );
        View rightImageView = view.findViewById( R.id.rightImage );

        initTitleAction( fragment.getContext(), 1, space, back, title, rightTitle, rightImageView, titleString, rightTitleString,
            0 );
    }

    private static void initTitleAction( Context context, int backMode, View space, View back, View title, View rightTextView,
        View rightImageView,
        CharSequence titleChars,
        @Nullable CharSequence rightTitleChars, @DrawableRes int rightImageRes ) {

        if ( space != null ) {
            space.getLayoutParams().height = getStatusBarHeight( context );
        }

        if ( back != null ) {

            back.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View view ) {

                    if ( view.getContext() instanceof Activity ) {
                        if ( backMode == 0 ) {
                            ((Activity) view.getContext()).finish();
                        } else {
                            ((Activity) view.getContext()).onBackPressed();
                        }
                    }
                }
            } );

            if ( backMode == -1 ) {
                back.setVisibility( View.GONE );
            }
        }

        if ( title != null && !TextUtils.isEmpty( titleChars ) ) {
            TextView titleText = (TextView) title;
            titleText.setText( titleChars );
            titleText.requestFocus();
        }

        if ( !TextUtils.isEmpty( rightTitleChars ) && rightTextView != null ) {
            ((TextView) rightTextView).setText( rightTitleChars );
            rightTextView.setVisibility( View.VISIBLE );
        } else if ( rightTextView != null ) {
            rightTextView.setVisibility( View.INVISIBLE );
        }

        if ( rightImageView != null && rightImageRes != 0 ) {
            ((ImageView) rightImageView).setImageResource( rightImageRes );
            rightImageView.setVisibility( View.VISIBLE );
        } else if ( rightImageView != null ) {
            rightImageView.setVisibility( View.INVISIBLE );
        }

        /* 状态栏文字深色模式 */
        if ( context instanceof Activity ) {
            CommonCode.setStatusBarsLight( ((Activity) context), true );
        }
    }
}
